﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenQuarters.WebQuarters.Core;
using OpenQuarters.WebQuarters.Utils;
using OpenQuarters.WebQuarters.Modules.ItemList;
using RssToolkit.Rss;

namespace OpenQuarters.WebQuarters.Modules.Feedback
{
    [Serializable]
    [Newtonsoft.Json.JsonObject(Newtonsoft.Json.MemberSerialization.OptIn)]
    partial class Feedback : FeedbackEntity<Feedback, long>, IItemListable
    {
        public enum ModerationStatus
        {
            Unmoderated,
            Approved,
            Rejected
        }

        public override long GetKey(Feedback Entity)
        {
            return Entity.FeedbackId;
        }

        public override System.Linq.Expressions.Expression<Func<Feedback, bool>> GetIDSelector(long ID)
        {
            return e => e.FeedbackId == ID;
        }

        [Newtonsoft.Json.JsonProperty]
        [PropertyConfiguration(PropertyType.Hidden, SourceField = "FeedbackId")]
        public long json_FeedbackId
        {
            get { return this.FeedbackId; }
            set { this.FeedbackId = value; }
        }

        [Filterable, Orderable]
        [Newtonsoft.Json.JsonProperty]
        [PropertyConfiguration(PropertyType.Hidden, SourceField = "CMSPageId")]
        public long json_CMSPageId
        {
            get { return this.CMSPageId; }
            set { this.CMSPageId = value; }
        }

        [Filterable, Orderable]
        [Newtonsoft.Json.JsonProperty]
        [PropertyConfiguration(PropertyType.DropDown, SourceField = "Moderated")]
        public ModerationStatus json_Moderated
        {
            get { return this.Moderated; }
            set { this.Moderated = value; }
        }

        [Filterable, Orderable]
        [Newtonsoft.Json.JsonProperty]
        [PropertyConfiguration(PropertyType.Hidden, SourceField = "UserId")]
        public string json_UserId
        {
            get { return this.UserId; }
            set { this.UserId = value; }
        }

        [Filterable, Orderable]
        [Newtonsoft.Json.JsonProperty]
        [PropertyConfiguration(PropertyType.TextBox, SourceField = "Name")]
        public string json_Name
        {
            get { return this.Name; }
            set { this.Name = value; }
        }

        [Filterable]
        [Newtonsoft.Json.JsonProperty]
        [PropertyConfiguration(PropertyType.TextBox, SourceField = "Email")]
        public string json_Email
        {
            get { return this.Email; }
            set { this.Email = value; }
        }

        [Filterable]
        [Newtonsoft.Json.JsonProperty]
        [PropertyConfiguration(PropertyType.TextBox, SourceField = "Website")]
        public string json_Website
        {
            get { return this.Website; }
            set { this.Website = value; }
        }

        [Filterable, Orderable]
        [Newtonsoft.Json.JsonProperty]
        [PropertyConfiguration(PropertyType.TextBox, SourceField = "Score")]
        public decimal json_Score
        {
            get { return this.Score; }
            set { this.Score = value; }
        }

        [Newtonsoft.Json.JsonProperty]
        [PropertyConfiguration(PropertyType.TextBox, SourceField = "Comments")]
        public string json_Comments
        {
            get { return this.Comments; }
            set { this.Comments = value; }
        }

        [Filterable, Orderable]
        [Newtonsoft.Json.JsonProperty]
        [PropertyConfiguration(PropertyType.TextBox, SourceField = "DateCreated")]
        public DateTime json_DateCreated
        {
            get { return this.DateCreated; }
            set { this.DateCreated = value; }
        }

        //[Filterable, Orderable]
        //[Newtonsoft.Json.JsonProperty]
        //[PropertyConfiguration(PropertyType.TextBox, SourceField = "DateModerated")]
        //public DateTime? json_DateModerated
        //{
        //    get { return this.DateModerated; }
        //    set { this.DateModerated = value; }
        //}

        [Newtonsoft.Json.JsonProperty]
        [PropertyConfiguration(PropertyType.TextBox, SourceField = "AdditionalField1")]
        public string json_AdditionalField1
        {
            get { return this.AdditionalField1; }
            set { this.AdditionalField1 = value; }
        }

        [Newtonsoft.Json.JsonProperty]
        [PropertyConfiguration(PropertyType.TextBox, SourceField = "AdditionalField2")]
        public string json_AdditionalField2
        {
            get { return this.AdditionalField2; }
            set { this.AdditionalField2 = value; }
        }

        [Newtonsoft.Json.JsonProperty]
        [PropertyConfiguration(PropertyType.TextBox, SourceField = "AdditionalField3")]
        public string json_AdditionalField3
        {
            get { return this.AdditionalField3; }
            set { this.AdditionalField3 = value; }
        }

        [Newtonsoft.Json.JsonProperty]
        [PropertyConfiguration(PropertyType.TextBox, SourceField = "AdditionalField4")]
        public string json_AdditionalField4
        {
            get { return this.AdditionalField4; }
            set { this.AdditionalField4 = value; }
        }

        [Newtonsoft.Json.JsonProperty]
        [PropertyConfiguration(PropertyType.TextBox, SourceField = "AdditionalField5")]
        public string json_AdditionalField5
        {
            get { return this.AdditionalField5; }
            set { this.AdditionalField5 = value; }
        }

        public string EditLink
        {
            get
            {
                return ItemListModule.GetItemEditLink(this);
            }
        }

        public string DeleteLink
        {
            get
            {
                return ItemListModule.GetItemDeleteLink(this);
            }
        }

        #region IItemListable Members

        public bool IsNewItem()
        {
            return IsNew();
        }

        public IItemListable LoadItem(string ID)
        {
            return Load(ID.ToInt64());
        }

        public IItemListable SaveItem()
        {
            if (this.Moderated != ModerationStatus.Unmoderated && !this.DateModerated.HasValue)
                this.DateModerated = DateTime.Now;

            return this.Save();
        }

        public void DeleteItem()
        {
            this.Delete();
        }

        public string GetIDAsString()
        {
            return this.FeedbackId.ToString();
        }

        public IItemListable CreateItem<T>(ItemListerControl<T> parentList) where T : class, IItemListable
        {
            return new Feedback()
            {
                CMSPageId = CMSPage.Current.PageId,
                UserId = (OpenQuarters.WebQuarters.Core.User.Current.IsAuthenticated ? User.Current.UserId : null),
                DateCreated = DateTime.Now,
                Moderated = ModerationStatus.Unmoderated
            };
        }

        public IQueryable GetQueryableList()
        {
            if (OpenQuarters.WebQuarters.Core.User.Current.HasPermission(OpenQuarters.WebQuarters.Core.Permissions.CMSPage.Edit.SetValue(CMSPage.Current.PageId.ToString())))
                return DataContext.GetTable<Feedback>().OrderByDescending(f => f.Moderated).OrderByDescending(f => f.DateCreated);
            else
                return DataContext.GetTable<Feedback>().Where(f => f.Moderated == ModerationStatus.Approved).OrderByDescending(f => f.DateCreated);
        }

        public string ItemUrl()
        {
            StringBuilder sb = new StringBuilder();
            if (ItemListerHelper.CurrentFullViewPage != null)
            {
                sb.Append(ItemListerHelper.CurrentFullViewPage.GetUrl() + "/");
            }
            sb.Append(this.DateCreated.Year.ToString() + "/" + this.DateCreated.Month.ToString().PadLeft(2, Char.Parse("0")) + "/" + this.DateCreated.Day.ToString().PadLeft(2, Char.Parse("0")) + "/" + FeedbackId);

            return sb.ToString();
        }

        public IItemListable GetItemFromUrl(string url, List<IItemListable> additionalItems)
        {
            var parsedUrl = url.Split('/');
            return Load(Convert.ToInt64(parsedUrl[parsedUrl.Length - 1]));
        }

        public RssToolkit.Rss.RssItem ToRssItem()
        {
            CMSPage cmsPage = CMSPage.Load(this.CMSPageId);

            RssItem item = new RssItem();
            item.Title = "Feedback - " + this.DateCreated.ToString();
            item.PubDateParsed = this.DateCreated;
            item.Description = this.Comments;
            item.Link = "/site/" + cmsPage.Urls[0];

            RssCategory category = new RssCategory();
            category.Text = cmsPage.Title;
            item.Categories = new List<RssCategory>();
            item.Categories.Add(category);

            return item;
        }

        public string ItemTitle()
        {
            return "Comment by " + this.Name + " on " + this.DateCreated.ToShortDateString();
        }

        public string MetaKeywords()
        {
            return null;
        }

        public string MetaDescription()
        {
            return null;
        }

        #endregion

    }
}
